﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tododoo.Lib
{
    public static class Services
    {
        private static Dictionary<Type, Func<object>> _services = new Dictionary<Type, Func<object>>();

        /// <summary>
        /// Registers (and overwrites is already registered)
        /// </summary>
        /// <typeparam name="TService"></typeparam>
        /// <typeparam name="TImpl"></typeparam>
        /// <param name="impl"></param>
        public static void Register<TService>(Func<TService> impl)
        {
            if(impl == null) { throw new ArgumentNullException("impl");}
            Type serviceType = typeof(TService);

            if (_services.ContainsKey(serviceType))
            {
                _services.Remove(serviceType);
            }

            _services.Add(serviceType, () => impl());
        }

        public static TService Get<TService>()
        {
            if (_services.ContainsKey(typeof(TService)))
            {
                return (TService)_services[typeof(TService)]();
            }

            return default(TService);
        }
    }
}
